package com.hns.frame.service;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hns.can.common.entity.BaseEntity;
import com.hns.frame.dao.CommonEntityDao;
import com.hns.frame.dao.SqlMapDao;
import com.hns.frame.enumrate.MsgType;
import com.hns.frame.exception.BusinessException;
import com.hns.frame.mybatis.Pagination;
import com.hns.frame.mybatis.RowBoundsExt;
import com.hns.frame.security.AppUserDetailsImpl;
import com.hns.tool.pub.ContextHolderUtils;
import com.hns.tool.pub.PubUtil;

/**
 * 共通服务类
 * @author:Fisher
 * @email:zhuangcaijin@126.com
 * @version Revision 2.0.0
 */
@Service
public class CommonService{
    @Autowired
    public CommonEntityDao  commonEntityDao;
    
    @Autowired
    public SqlMapDao sqlMapDao;
    
    public AppUserDetailsImpl getCurrentUser() {
        try{
            return (AppUserDetailsImpl)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        }catch(ClassCastException ce){
        	throw new BusinessException(MsgType.NO_LOGON .getFlag(), "未登录，或回话过期");
        }
    }
    
    
    /**
     * app取得当前登录用户
     * @return  登录用户的基本信息
     * @author:Fisher
     * @email:zhuangcaijin@126.com
     */
    public AppUserDetailsImpl getAppCurrentUser() {
        try{
            return (AppUserDetailsImpl)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        }catch(ClassCastException ce){
        	return null;
        }
    }
    
    
    /**
     * 公用保存service
     * @param entity
     * @author:Fisher
     * @email:zhuangcaijin@126.com
     */
    @Transactional
    public void save(Object entity) {
        commonEntityDao.save(entity);
    }
    
    /**
     * 公用根据id删除数据service
     * @param id
     * @param clazz
     * @author:Fisher
     * @email:zhuangcaijin@126.com
     */
    @SuppressWarnings({"unchecked", "rawtypes" })
    @Transactional
    public void delete(String id, Class clazz){
        commonEntityDao.delete(clazz, new String[]{id});
    }
    
    /**
     * 公用根据实体删除数据service
     * @param entity
     * @author:Fisher
     * @email:zhuangcaijin@126.com
     */
    @Transactional
    public void delete(Object entity){
        commonEntityDao.delete(entity);
    }
    
    /**
     * 公用更新数据service
     * @param entity
     * @author:Fisher
     * @email:zhuangcaijin@126.com
     */
    @Transactional
    public void update(Object entity){
        commonEntityDao.update(entity);
    }
    
    /**
     * 根据Id查询数据
     * @param id
     * @param clazz
     * @return
     * @author:Fisher
     * @email:zhuangcaijin@126.com
     */
    @SuppressWarnings({"rawtypes", "unchecked" })
    public Object findById(String id, Class clazz){
        return commonEntityDao.find(id, clazz);
    }
    
    /**
     * ibatis 的分页查询
     * @param countStatement 用于统计的sql
     * @param queryStatement 用于查询的sql
     * @param param 查询参数
     * @param clazz 转化的类
     * @return 翻页对象集合
     * @author:Fisher
     * @email:zhuangcaijin@126.com
     */
    @SuppressWarnings({"unchecked", "rawtypes" })
    public Pagination myBatisPage(String countStatement, String queryStatement, Object param, Class clazz) {
        RowBoundsExt rowBounds = this.getRowBounds();
        int count = (Integer)sqlMapDao.selectOne(countStatement, param);
        Pagination page = new Pagination(rowBounds, count);
        List list = sqlMapDao.selectList(queryStatement, param, rowBounds);
        
        page.setData(PubUtil.convertListMapToObject(list, clazz));
        return page;
    }
    
    /**
     * @param countStatement    用于统计的sql
     * @param queryStatement    用于查询的sql
     * @param param 查询参数
     * @return  翻页对象集合
     * @author:Fisher
     * @email:zhuangcaijin@126.com
     * @创建日期:2014-9-22
     * @功能说明：
     */
    public Pagination myBatisPage(String countStatement, String queryStatement, Object param) {
        RowBoundsExt rowBounds = this.getRowBounds();
        int count = (Integer)sqlMapDao.selectOne(countStatement, param);
        Pagination page = new Pagination(rowBounds, count);
        List list = sqlMapDao.selectList(queryStatement, param, rowBounds);
        
        page.setData(list);
        return page;
    }
    
    /**
     * 通过request获取表格的请求页数对象
     * @return
     * @author:Fisher
     * @email:zhuangcaijin@126.com
     */
    public RowBoundsExt getRowBounds() {
        HttpServletRequest request = ContextHolderUtils.getRequest(); 
        RowBoundsExt rowBounds;
        if(request != null) {
            Integer page = request.getParameter("page") != null ? Integer.valueOf(request.getParameter("page")) : 1;
            Integer pageSize = request.getParameter("rows") != null ? Integer.valueOf(request.getParameter("rows")) : 10;
            String sortName = request.getParameter("sidx");
            String asc = request.getParameter("sord");
            rowBounds = new RowBoundsExt(page, pageSize, sortName, asc);
        }else {
            rowBounds = new RowBoundsExt(0, 10);
        }
        return rowBounds;
    }
	
    /**
     * 后台校验重复方法
     * @param sql   校验的sql脚本
     * @param entity    校验的实体参数
     * @param exp   提示校验信息    例： "用户名已经存在"
     * @author:Fisher
     * @email:zhuangcaijin@126.com
     */
    public void check(String countCheckStatement, BaseEntity entity, String exp){
        Integer count = (Integer) sqlMapDao.selectOne(countCheckStatement, entity);
        if(PubUtil.isNotEmpty(count) && count > 0){
            throw new BusinessException(MsgType.WARN.getFlag(),exp);
        }
    }
}
